package com.flyme.module.file.provider.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.file.client.api.SysOffCourseRemoteService;
import com.flyme.module.file.client.entity.SysOffCourse;
import com.flyme.module.file.provider.service.SysOffCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author flyme
 */
@Slf4j
@Api(tags="线下课程")
@RestController
public class SysOffCourseController extends BaseController<SysOffCourseService,SysOffCourse> implements SysOffCourseRemoteService {

    /**
     * 课程列表
     *
     * @return
     */
    @ApiOperation(value = "获取课程分页列表", notes = "获取课程分页列表")
    @Override
    public ResultBody list(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo,SysOffCourse offCourse) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("oc.id,recomIndex,courseTitle,courseImage,oc.deadline,applyType,enrollNum,courseStatus,coursePrice,courseBeginDate,courseAddress,oc.createTime,user.userName, (select count(*) from sys_courseenroll ce where ce.targetId=oc.id) nowNum");
        cq.like("courseTitle",offCourse.getCourseTitle());
        cq.ge("courseBeginDate",offCourse.getStartDate());
        cq.le("courseBeginDate",offCourse.getEndDate());
        cq.createAlias("user","user");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }
    @ApiOperation(value = "添加课程", notes = "添加课程")
    @Override
    public ResultBody add(@ModelAttribute(value = "course") SysOffCourse offCourse) {
        boolean result = bizService.add(offCourse);
        return ResultBody.success(result);
    }

    @ApiOperation(value = "通过id查询课程")
    @Override
    public ResultBody get(@PathVariable String id) {
        SysOffCourse offCourse = bizService.getById(id);
        return ResultBody.success().setData(offCourse);
    }

    @ApiOperation(value = "编辑课程", notes = "编辑课程")
    @Override
    public ResultBody edit(SysOffCourse offCourse) {
        boolean result= bizService.edit(offCourse);
        return ResultBody.success(result);
    }

    @ApiOperation(value = "删除课程", notes = "删除课程")
    @Override
    public ResultBody delByIds(@RequestParam("ids") String[] ids) {
        return bizService.delByIds(ids);
    }

    /**
     * 更新课程状态
     *
     * @param id 课程ID
     *@param courseStatus 是否上架
     * @return
     */
    @ApiOperation(value = "更新课程状态", notes = "更新课程状态")
    @Override
    public ResultBody setOffCourseStatus(@RequestParam("id") Long id, @RequestParam("courseStatus") Integer courseStatus) {
        boolean result = bizService.update(new UpdateWrapper<SysOffCourse>().set("courseStatus",courseStatus).eq("id",id));
        return ResultBody.ok("设置成功").setData(result);
    }

    /**
     * 更新首页推荐
     *
     * @param id 课程ID
     *@param recomIndex 是否推荐到首页
     * @return
     */
    @ApiOperation(value = "更新首页推荐", notes = "更新首页推荐")
    @Override
    public ResultBody setOffCourseIndex(@RequestParam("id") Long id, @RequestParam("recomIndex") Integer recomIndex) {
        boolean result = bizService.update(new UpdateWrapper<SysOffCourse>().set("recomIndex",recomIndex).eq("id",id));
        return ResultBody.ok("设置成功").setData(result);
    }

    /**
     * 更新报名状态
     *
     * @param id 课程ID
     *@param applyType 报名状态
     * @return
     */
    @ApiOperation(value = "更新课程状态", notes = "更新课程状态")
    @Override
    public ResultBody setApplyType(@RequestParam("id") Long id, @RequestParam("applyType") Integer applyType) {
        boolean result = bizService.update(new UpdateWrapper<SysOffCourse>().set("applyType",applyType).eq("id",id));
        return ResultBody.ok("设置成功").setData(result);
    }
}
